package service;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
 * @author Gleb
 */
public class PropertiesManager
{
    private static String DBPath = "";
    private static String DBLogin = "";
    private static String DBPassword = "";
    private static String XMLPath = "config/hibernate.cfg.xml";
    public static String getDBPath()
    {
        DBPath = findAttribute("connection.url").getTextContent();
        return DBPath;
    }
    public static String getDBLogin()
    {
        DBLogin = findAttribute("connection.username").getTextContent();
        return DBLogin;
    }
    public static String getDBPassword()
    {
        DBPassword = findAttribute("connection.password").getTextContent();
        return DBPassword;
    }
    public static boolean setDBPath(String path)
    {
        if (!DBPath.equals(path))
        {
            DBPath = path;
            return saveChanges(findAttribute("connection.url"),DBPath);
        }
        return true;
    }

    public static boolean setDBLogin(String login)
    {
        if (!DBLogin.equals(login))
        {
            DBLogin = login;
            return saveChanges(findAttribute("connection.username"),DBLogin);
        }
        return true;
    }
    public static boolean setDBPassword(String password)
    {
         if (!DBPassword.equals(password))
        {
            DBPassword = password;
            return saveChanges(findAttribute("connection.password"),DBPassword);
        }
        return true;
    }
    /*
     * this method return true if configuration XML file is exist
     */
    public static boolean isOk() 
    { 
        if (getParseDoc() == null)
        {
            return false;
        }
        return true;
    }
    private static Node findAttribute(String atr)
    {
           Document doc = getParseDoc();
           if ( doc != null )
           {
               NodeList nl = doc.getElementsByTagName("property");
               for (int i = 0 ; i < nl.getLength(); i++)
               {
                   NamedNodeMap nnm = nl.item(i).getAttributes();
                   if (nnm.getNamedItem("name").getNodeValue().equals(atr))
                   {
                       return nl.item(i);
                   }
               }
           }
           return null;
    }
    private static boolean saveChanges(Node n, String value)
    {
            Transformer trans;
            try
            {
                trans = TransformerFactory.newInstance().newTransformer();
                trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
                trans.setOutputProperty(OutputKeys.INDENT, "yes");
                trans.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,"-//Hibernate/Hibernate Configuration DTD 3.0//EN");
        trans.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd");

                //Document doc = getParseDoc();
               // if ( doc != null )
              //  {
                    
                    if (n != null)
                    {
                         n.setTextContent(value);
                         DOMSource source = new DOMSource(n.getOwnerDocument());
                         StreamResult result = new StreamResult(XMLPath);
                         try
                         {
                            trans.transform(source, result);
                         }
                         catch (TransformerException ex) {return false;}
                    }
                    else return false;
             //   }
              //  else return false;
                return true;
            }
            catch (TransformerConfigurationException ex) {return false;}

    }
    private static String getValue(String tag)
    {
         Document doc = getParseDoc();
        if ( doc != null )
        {
           NodeList nl = doc.getElementsByTagName(tag);

           if (nl.item(0) != null)
           {
                return nl.item(0).getTextContent();
           }
        }
        return null;
    }
    private static Document getParseDoc()
    {
        File file = new File(XMLPath);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db;
            try
            {
                db = dbf.newDocumentBuilder();

                try
                {
                    Document doc = db.parse(new File(XMLPath));
                    return doc;
                }
                catch (SAXException ex) {return null;}
                catch (IOException ex)  {return null;}
            }
            catch (ParserConfigurationException ex){return null;}
            
    }
}
